function clearSelect(sid) {
	if (sid == "provinces") {
		$("#" + sid).empty().append("<option value='0'>--选择省份--</option>");
	} else if (sid == "citys") {
		$("#" + sid).empty().append("<option value=''>--选择城市--</option>");
	} else {
		$("#" + sid).empty().append("<option value=''>--选择区/县--</option>");
	}

}
// 进行遍历存值
function fillSelect_sort(sid, address) {

	if (!address || !sid) {
		return;
	}
	var $select = $("#" + sid);
	$select.empty();
	if (address.length != 1) {
		if (sid == "provinces") {
			$select.append("<option value='0'>--选择省份--</option>");
		} else if (sid == "citys") {
			$select.append("<option value=''>--选择城市--</option>");
		} else {
			$select.append("<option value=''>--选择区/县--</option>");
		}

	}
	 
	var proCode = $("#proCodeHidden").val();
	var cityCode = $("#cityCodeHidden").val(); 
	var areaCode = $("#areaCodeHidden").val(); 
	for (var i = 0; i < address.length; i++) {
		$select.append("<option value='" + address[i].code + "'>"+ address[i].name + "</option>");
		if (proCode == address[i].code) {
			$("#" + sid).find("option[value=" + proCode + "]").attr("selected",true);
		}
		if (cityCode == address[i].code) {
			$("#" + sid).find("option[value=" + cityCode + "]").attr("selected", true);
		}
		if (areaCode == address[i].code) {
			$("#" + sid).find("option[value=" + areaCode + "]").attr("selected", true);
		}
	}

}
function loadProvince(isNext) { 
	clearSelect("provinces");
	AddressQuery.loadProvices(function(objs) {

		clearSelect("citys");
		clearSelect("areas");
		fillSelect_sort("provinces", objs);
		if(isNext){ ///是否加载城市
			loadCity();
		}
		
	});
}
function loadCity(proCode) { 
	var sortCode = "";
	if(proCode==null){
		sortCode=$("#provinces").val();
		if(sortCode==null || sortCode==""){//为空则是初次进入
			loadProvince(true);
			return;
		}
	}else{
		sortCode=proCode;
	}
	
	if (!sortCode) {
		return;
	}
	AddressQuery.loadCitys(sortCode, function(sortSmall) {
		clearSelect("areas");
		fillSelect_sort("citys", sortSmall);
		loadArea();
	});
}
function loadArea() {
	var sortCode = $("#citys").val();
	if (!sortCode) {
		return;
	}
	AddressQuery.loadAreas(sortCode, function(sortSmall) {

		fillSelect_sort("areas", sortSmall);

	});
}

/**
 * 校验地址信息
 */
function validateAddress(){
	var provinces = $("#provinces").val();
	if(provinces==""){
		alert("请选择省份");
		return false;
	}
	var citys = $("#citys").val();
	if(citys ==""){
		alert("请选择城市");
		return false;
	}
	var area = $("#areas").val();
	if(area == ""){
		alert("请选择地区");
		//$("#areas").append("<p id='pAreas'>请选择地区</p>");
		//setTimeout($("#pAreas").remove(),'3000')
		return false;
	}
	return true;
}